package com.chuanye.jiguangimdemo.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.utils.TimeFormat;

import java.util.List;

import cn.jpush.im.android.api.content.CustomContent;
import cn.jpush.im.android.api.content.PromptContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.Message;

public class MessageAdapter extends BaseAdapter {
    private Context mContext;
    private List<Conversation> conversationList;


    public MessageAdapter(Context mContext, List<Conversation> conversationList) {
        this.mContext = mContext;
        this.conversationList = conversationList;
    }

    @Override
    public int getCount() {
        return conversationList.size();
    }

    @Override
    public Object getItem(int position) {
        return conversationList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = View.inflate(mContext, R.layout.item_message_view,null);
            holder = new ViewHolder();
            holder.mess_item_icon_iv = convertView.findViewById(R.id.mess_item_icon_iv);
            holder.mess_item_newmsg_number_tv = convertView.findViewById(R.id.mess_item_newmsg_number_tv);
            holder.mess_item_username_tv = convertView.findViewById(R.id.mess_item_username_tv);
            holder.mess_item_content_tv = convertView.findViewById(R.id.mess_item_content_tv);
            holder.mess_item_time_tv = convertView.findViewById(R.id.mess_item_time_tv);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Conversation conversation = conversationList.get(position);
        holder.mess_item_icon_iv.setImageResource(R.drawable.rc_default_portrait);

        holder.mess_item_username_tv.setText(conversation.getTitle());
        Message message = conversation.getLatestMessage();
        if (message != null){
            TimeFormat timeFormat = new TimeFormat(mContext, message.getCreateTime());
            //会话界面时间
            holder.mess_item_time_tv.setText(timeFormat.getTime());
            String contentStr;
            switch (message.getContentType()) {
                case image:
                    contentStr = mContext.getString(R.string.type_picture);
                    break;
                case voice:
                    contentStr = mContext.getString(R.string.type_voice);
                    break;
                case location:
                    contentStr = mContext.getString(R.string.type_location);
                    break;
                case file:
                    String extra = message.getContent().getStringExtra("video");
                    if (!TextUtils.isEmpty(extra)) {
                        contentStr = mContext.getString(R.string.type_smallvideo);
                    } else {
                        contentStr = mContext.getString(R.string.type_file);
                    }
                    break;
                case video:
                    contentStr = mContext.getString(R.string.type_video);
                    break;
                case eventNotification:
                    contentStr = mContext.getString(R.string.group_notification);
                    break;
                case custom:
                    CustomContent customContent = (CustomContent) message.getContent();
                    Boolean isBlackListHint = customContent.getBooleanValue("blackList");
                    if (isBlackListHint != null && isBlackListHint) {
                        contentStr = mContext.getString(R.string.jmui_server_803008);
                    } else {
                        contentStr = mContext.getString(R.string.type_custom);
                    }
                    break;
                case prompt:
                    contentStr = ((PromptContent) message.getContent()).getPromptText();
                    break;
                default:
                    contentStr = ((TextContent) message.getContent()).getText();
                    break;
            }
            holder.mess_item_content_tv.setText(contentStr);
        }

        if (conversation.getUnReadMsgCnt() == 0){
            holder.mess_item_newmsg_number_tv.setVisibility(View.GONE);
        }else {
            if (conversation.getType().equals(ConversationType.single)) {
                holder.mess_item_newmsg_number_tv.setVisibility(View.VISIBLE);
                if (conversation.getUnReadMsgCnt() < 100) {
                    holder.mess_item_newmsg_number_tv.setText(String.valueOf(conversation.getUnReadMsgCnt()));
                } else {
                    holder.mess_item_newmsg_number_tv.setText("99+");
                }

            }

        }

        return convertView;
    }


    public static class ViewHolder{
        ImageView mess_item_icon_iv;//头像
        TextView mess_item_newmsg_number_tv;//新消息数
        TextView mess_item_username_tv;//用户名
        TextView mess_item_content_tv;//聊天内容
        TextView mess_item_time_tv;//时间
    }
}
